/**
 *
 */
package com.ce.pms.house.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IMeterDao;
import com.ce.pms.house.model.MeterDomain;
import com.ce.pms.house.model.MeterType;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: MeterDaoImpl
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 付湘辉 Created on： 2008-7-8
 * @author update on：
 */
public class MeterDaoImpl extends PmsBaseDaoImpl<MeterDomain> implements
		IMeterDao {

	/**
	 *
	 * 功能描述: 添加走表
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param meterDomain
	 * @return MeterDomain 返回结果
	 * @roseuid 485A28CD0109
	 */
	public MeterDomain addMeter(MeterDomain meterDomain) {
		return super.saveObject(meterDomain);
	}

	/**
	 * 功能描述: 执行一个批量增加走表的原始SQL语句 <br>
	 *
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-23 <br>
	 * Edit author: <br>
	 * Edit on: <br>
	 * Why: <br>
	 */
	public Boolean addMeterBatch(List<Object[]> paramList) {
		String sql = "INSERT INTO HM_METER(NAME,BASE,METER_TYPE_ID,HOUSE_ID,VALIDITY,CREATOR,SYSDATE) VALUES(?,0,?,?,'1',?,?) ";
		super.executeSQLBatch(sql, paramList);
		return true;
	}

	/**
	 * 方法描述: 根据走表类型ID查询走表归零值
	 *
	 * @author 胡煦 Create on:2008-8-18
	 * @param meterTypeId
	 * @return BigInteger Edit author: Edit on: 修改原因：
	 */
	public BigInteger getMAXZByMeterTypeCode(String meterTypeCode) {
		String sql = "SELECT MAXZ FROM HM_METER_RESET_DEF mr left join HM_METER_TYPE mt on mr.METER_TYPE_ID = mt.METER_TYPE_ID WHERE mt.CODE = ?";
		Query query = super.createSQLQuery(sql, new Object[] { meterTypeCode });
		return (BigInteger) query.uniqueResult();
	}

	/**
	 *
	 * 功能描述: 通过走表类型id查询走表类型实体
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param meterTypeId
	 * @return MeterType 返回结果
	 */
	public MeterType getMeterTypeById(Long meterTypeId) {
		if (BlankUtil.isBlank(meterTypeId)) {
			return null;
		}

		String queryStr = "FROM MeterType mt WHERE mt.keyword=? ";

		Query query = super.createQuery(queryStr, new Object[] { meterTypeId });

		return (MeterType) query.uniqueResult();

	}

	/**
	 *
	 * 功能描述: 通过走表类型code查询走表类型实体
	 *
	 * @author 胡煦 Created on:2008-8-8
	 *
	 * 修改原因：
	 * @param meterTypeCode
	 * @return MeterType 返回结果
	 */
	public MeterType getMeterTypeByCode(String meterTypeCode) {
		if (BlankUtil.isBlank(meterTypeCode)) {
			return null;
		}

		String queryStr = "FROM MeterType mt WHERE mt.code=? ";

		Query query = super.createQuery(queryStr,
				new Object[] { meterTypeCode });

		return (MeterType) query.uniqueResult();

	}

	/**
	 *
	 * 功能描述: 通过走表类型name查询走表类型实体
	 *
	 * @author 胡煦 Created on:2008-8-8
	 *
	 * 修改原因：
	 * @param meterTypeName
	 * @return MeterType 返回结果
	 */
	public MeterType getMeterTypeByName(String meterTypeName) {
		if (BlankUtil.isBlank(meterTypeName)) {
			return null;
		}

		String queryStr = "FROM MeterType mt WHERE mt.name=? ";

		Query query = super.createQuery(queryStr,
				new Object[] { meterTypeName });

		return (MeterType) query.uniqueResult();

	}

	/**
	 * 方法描述:获取某个楼栋下所有某类有效走表的底数
	 *
	 * @author 胡煦 Create on:2008-12-7
	 * @param buildingId,meterTypeCode
	 * @return List<Object[]> Edit author: Edit on: 修改原因：
	 */
	public List<Object[]> getMeterBaseByBuildingId(Long buildingId, String meterTypeCode) {
		if (BlankUtil.isBlank(buildingId)) {
			return null;
		}

		// String queryStr = "select md.house.id,md.base FROM MeterDomain md
		// WHERE md.validity = 1 and md.house.building.id = ? and
		// md.meterType.code = ? ";
		StringBuffer queryStr = new StringBuffer
		("select meterdomai0_.HOUSE_ID as col_0_0_, meterdomai0_.BASE as col_1_0_ from HM_METER meterdomai0_, HM_HOUSE housedomai1_ inner join HM_PRODUCT housedomai1_1_ on ").
		append("housedomai1_.HOUSE_ID=housedomai1_1_.PRODUCT_ID, HM_METER_TYPE metertype2_ where meterdomai0_.HOUSE_ID=housedomai1_.HOUSE_ID and ").
		append("meterdomai0_.METER_TYPE_ID=metertype2_.METER_TYPE_ID and meterdomai0_.VALIDITY=1 and housedomai1_.BUILDING_ID=? and metertype2_.CODE=? ");
		Query query = super.createSQLQuery(queryStr.toString(), new Object[] { buildingId,
				meterTypeCode });

		return query.list();
	}

	/**
	 *
	 * 功能描述: 通过房号id查询走表实体List
	 *
	 * @author 胡煦 Created on:2008-7-14
	 *
	 * 修改原因：
	 * @param houseId
	 * @return MeterDomain 返回结果
	 */
	public List<MeterDomain> getMeterListByHouseId(Long houseId) {

		if (BlankUtil.isBlank(houseId)) {
			return null;
		}

		List<MeterDomain> list = new ArrayList<MeterDomain>();

		String queryStr = "FROM MeterDomain md WHERE md.house.id = ? ";

		Query query = super.createQuery(queryStr, houseId);

		list = query.list();

		return list;

	}

	/**
	 * 功能描述: 得到指定房屋和走表类型的走表数量 <br>
	 *
	 * @param houseId
	 * @param meterType
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-22 <br>
	 * Edit author: <br>
	 * Edit on: <br>
	 * Why: <br>
	 */
	public Long getMeterCountBy(Long houseId, Long meterType) {

		if (BlankUtil.isBlank(houseId) || BlankUtil.isBlank(meterType)) {
			throw new BusinessException("common:parameters_null_exception, ",
					BusinessException.WARNING);
		}

		Long meterCount = 0l;
		String queryStr = "SELECT COUNT(*) FROM MeterDomain md WHERE md.house.id=? AND md.meterType.id=? ";
		Query query = super.createQuery(queryStr, houseId, meterType);

		meterCount = (Long) query.uniqueResult();
		return meterCount;
	}

	/**
	 * 方法描述: 根据房屋id分页查询其下所有走表资料
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param qc
	 * @param pageNo
	 * @param pageSize
	 * @return page Edit author: Edit on: 修改原因：
	 */
	public Page queryPageMeterByHouseId(Long houseId, int pageNo, int pageSize) {
		if (BlankUtil.isBlank(houseId)) {
			return null;
		}
		String queryStr = "FROM MeterDomain md WHERE md.house.id = ? order by md.sysInfo.sysDate desc";

		return this.pagedQueryByPageNo(queryStr, pageNo, pageSize,
				new Object[] { houseId });

	}

	/**
	 *
	 * 功能描述: 通过房屋id和走表类型id查询走表实体
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param houseId
	 * @param meterTypeId
	 * @return MeterDomain 返回结果
	 */
	public MeterDomain getMeterByHouseId(Long houseId, Long meterTypeId) {
		if (BlankUtil.isBlank(houseId)) {
			return null;
		}
		Criteria cri = super.crtieriaFindBy("house.keyword", houseId);
		cri.add(Restrictions.eq("meterType.keyword", meterTypeId));
		List<MeterDomain> list = super.queryObjects(cri);

		if (list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/**
	 *
	 * 功能描述: 通过房屋id和走表类型code查询走表实体
	 *
	 * @author 胡煦 Created on:2008-8-8
	 *
	 * 修改原因：
	 * @param houseId
	 * @param meterTypeCode
	 * @return MeterDomain 返回结果
	 */
	public MeterDomain getMeterByHouseId(Long houseId, String meterTypeCode) {
		if (BlankUtil.isBlank(houseId)) {
			return null;
		}
		String queryStr = "FROM MeterDomain md WHERE md.house.id = ? and md.meterType.code = ? and md.validity = '1' order by md.id";
		Query query = super.createQuery(queryStr, houseId, meterTypeCode);
		List<MeterDomain> list = query.list();

		if (list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/**
	 *
	 * 功能描述: 通过楼栋id和走表类型code查询走表实体List
	 *
	 * @author 胡煦 Created on:2008-8-8
	 *
	 * 修改原因：
	 * @param buildingId
	 * @param meterTypeCode
	 * @return Map 返回结果
	 */
	public Map getMeterByBuildingId(Long buildingId, String meterTypeCode) {
		if (BlankUtil.isBlank(buildingId)) {
			return null;
		}
		String queryStr = "SELECT md.house.id,md FROM MeterDomain md WHERE md.house.building.id = ? and md.meterType.code = ? and md.validity = '1' order by md.id";
		Query query = super.createQuery(queryStr, buildingId, meterTypeCode);
		List<Object[]> list = query.list();
		Map map = new HashMap();
		for (Object[] objects : list) {
			map.put(objects[0], objects[1]);
		}
		return map;
	}
	/**
	 *
	 * 功能描述: 通过房间查询有效的走表实体List
	 *
	 * @author 胡煦 Created on:2008-8-8
	 *
	 * 修改原因：
	 * @param houseId
	 * @return List<MeterDomain> 返回结果
	 */
	public List<MeterDomain> getMeterListByhouseId(Long houseId) {
		if (BlankUtil.isBlank(houseId)) {
			return null;
		}
		String queryStr = "FROM MeterDomain md WHERE md.house.id = ? and md.validity = '1' order by md.id";
		Query query = super.createQuery(queryStr,houseId);
		return query.list();
	}

	/**
	 *
	 * 功能描述: 查询走表类型列表
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @return List<MeterType> 返回结果
	 */
	public List<MeterType> getMeterTypeList() {
		List<MeterType> list = new ArrayList<MeterType>();
		String queryStr = "FROM MeterType mt ORDER BY mt.code ";
		list = super.findAllResult(queryStr, new Object[] {});
		return list;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ce.pms.house.dao.IMeterDao#updateMeter(com.ce.pms.house.model.MeterDomain)
	 */
	public void updateMeter(MeterDomain meterDomain) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ce.pms.house.dao.IMeterDao#updateMeterDosage(com.ce.pms.house.model.MeterDomain)
	 */
	public void updateMeterDosage(MeterDomain meterDomain) {
		// TODO Auto-generated method stub

	}

}
